/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.test.mock.resource;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.omorphdb.core.io.Appender;
import org.omorphdb.core.io.Constants;

abstract class ByteArrayAppender implements Appender
{
	private final ByteBuffer buffer;

	ByteArrayAppender(byte[] array, int limit)
	{
		this.buffer = ByteBuffer.wrap(array);
		this.buffer.position(limit);
	}

	@Override
	public long getCursor() throws IOException
	{
		return this.buffer.position();
	}

	protected void setCursor(long position) throws IOException
	{
		this.buffer.position((int) position);
	}

	@Override
	public void appendString(String string) throws IOException
	{
		appendStringAsBytes(string.getBytes());
	}

	@Override
	public void appendStringAsBytes(byte[] bytes) throws IOException
	{
		this.buffer.putInt(bytes.length);
		this.buffer.put(bytes);
	}

	@Override
	public void appendDouble(double d) throws IOException
	{
		this.buffer.putDouble(d);
	}

	@Override
	public void appendFloat(float f) throws IOException
	{
		this.buffer.putFloat(f);
	}

	@Override
	public void appendLong(long l) throws IOException
	{
		this.buffer.putLong(l);
	}

	@Override
	public void appendInt(int i) throws IOException
	{
		this.buffer.putInt(i);
	}

	@Override
	public void appendShort(short s) throws IOException
	{
		this.buffer.putShort(s);
	}

	@Override
	public void appendChar(char c) throws IOException
	{
		this.buffer.putChar(c);
	}

	@Override
	public void appendByte(byte b) throws IOException
	{
		this.buffer.put(b);
	}

	@Override
	public void appendBoolean(boolean b) throws IOException
	{
		this.buffer.put(b ? Constants.TRUE : Constants.FALSE);
	}

	@Override
	public void appendType(byte type) throws IOException
	{
		appendByte(type);
	}
}
